package com.lzhl.buildjar.ui.support;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2015/12/8
 */
public class FileProvider implements SuggestProvider {
    private boolean onlyDir = false;

    public FileProvider() {
    }

    public FileProvider(boolean onlyDir) {
        this.onlyDir = onlyDir;
    }

    public List<String> suggest(String text) {
        LinkedList<String> suggests = new LinkedList<String>();

        text = text.trim();
        File file = new File(text);
        File parent;
        if(text.endsWith(File.separator)) {
            parent = file;
        }
        else {
            parent = file.getParentFile();
        }

        if(parent != null) {
            File[] files = parent.listFiles();
            if (files != null) {
                for (File aFile : files) {
                    if(aFile.getAbsolutePath().toLowerCase().startsWith(text.toLowerCase())) {
                        if(onlyDir) {
                            if(aFile.isDirectory()) {
                                suggests.addLast(aFile.getAbsolutePath());
                            }
                        }
                        else {
                            suggests.addLast(aFile.getAbsolutePath());
                        }
                    }
                }
            }
        }
        return suggests;
    }
}
